/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.impl;

import com.ivanbiz.dao.LockDateDAO;
import com.ivanbiz.model.HibernateUtil;
import com.ivanbiz.model.LockDate;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import org.hibernate.Session;

/**
 *
 * @author User
 */
public class LockDateDAOImpl extends GenericDAOImpl implements LockDateDAO {

    
    public void lockAndCheckToday(Date date) throws Exception {
            
        try {          
            HibernateUtil.beginTransaction();            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String convDate = sdf.format(date);
            Session session = HibernateUtil.getSession();
            LockDate lockDate = (LockDate)session.createQuery("from com.ivanbiz.model.LockDate h").uniqueResult();            
            Date date1 = new Date();       
            if(date1.getDate()-lockDate.getHoldDate().getDate()>1){
               int a = JOptionPane.showConfirmDialog(null,"Tanggal Pada Komputer Telah Melebihi 1 Hari Dari Transaksi Terahkir\n Mohon Di Konfirmasi Tanggal dengan hari ini\n Lanjut?");
              if(a==1 || a==2){
                  System.exit(0);
              }
              
                //  JOptionPane.showMessageDialog(null, "Tanggal Pada Komputer Telah Melebihi 1 Hari Dari Transaksi Terahkir");                
            }
            if(lockDate.getHoldDate().after(date1)){
                JOptionPane.showMessageDialog(null, "Tanggal Pada Komputer Expired");
                System.exit(0);
            }else{
              if(lockDate.getHoldDate().getDate() != date1.getDate()){                
                 Date date2 = new Date();                
                 date2.setHours(0);
                 date2.setMinutes(0);
                 date2.setSeconds(0);                 
                 lockDate.setHoldDate(date2);                 
                 session.update(lockDate);
              }                
            }
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }      
     }

    public Date getDateToday() throws Exception {        
        LockDate lockDate = null;
        try {                  
            Session session = HibernateUtil.getSession();
            lockDate = (LockDate)session.createQuery("from com.ivanbiz.model.LockDate h").uniqueResult();           
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }      
        return lockDate.getHoldDate();
    }

}
